home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / Xpm / pixmap / CutAndPaste.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  231 lines

  1. /* * Last edited: Dec 19 15:39 1991 (mallet) */
  2. /*
  3.  * $Id: CutAndPaste.c,v 1.2.1.2 1992/01/02 10:45:04 mallet Exp $
  4.  * 
  5.  * Copyright 1991 Lionel Mallet
  6.  * 
  7.  * Permission to use, copy, modify, distribute, and sell this software and its
  8.  * documentation for any purpose is hereby granted without fee, provided that
  9.  * the above copyright notice appears in all copies and that both that
  10.  * copyright notice and this permission notice appear in supporting
  11.  * documentation, and that the name of Lionel MALLET not be used in
  12.  * advertising or publicity pertaining to distribution of the software
  13.  * without specific, written prior permission.  Lionel MALLET makes no
  14.  * representations about the suitability of this software for any
  15.  * purpose.  It is provided "as is" without express or implied warranty.
  16.  *
  17.  * Lionel MALLET DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  18.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19.  * FITNESS, IN NO EVENT SHALL Lionel MALLET BE LIABLE FOR ANY SPECIAL,
  20.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  21.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 
  22.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  23.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  *  This software is opened and free. Furthermore, everybody is kindly
  26.  * invited to participate to improve it for the benefit of all.
  27.  * Improvements can be new features, bugs fixes and porting issues
  28.  * resolution.
  29.  *
  30.  * Author:  Lionel Mallet, SIMULOG
  31.  */
  32.  
  33. /*
  34.  * $XConsortium: CutAndPaste.c,v 1.1 90/06/09 20:20:17 dmatic Exp $
  35.  *
  36.  * Copyright 1989 Massachusetts Institute of Technology
  37.  *
  38.  * Permission to use, copy, modify, distribute, and sell this software and its
  39.  * documentation for any purpose is hereby granted without fee, provided that
  40.  * the above copyright notice appear in all copies and that both that
  41.  * copyright notice and this permission notice appear in supporting
  42.  * documentation, and that the name of M.I.T. not be used in advertising or
  43.  * publicity pertaining to distribution of the software without specific,
  44.  * written prior permission.  M.I.T. makes no representations about the
  45.  * suitability of this software for any purpose.  It is provided "as is"
  46.  * without express or implied warranty.
  47.  *
  48.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  49.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  50.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  51.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  52.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  53.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  54.  *
  55.  * Author:  Davor Matic, MIT X Consortium
  56.  */
  57.  
  58. #include <X11/IntrinsicP.h>
  59. #include <X11/Xmu/Converters.h>
  60. #include <X11/StringDefs.h>
  61. #include <X11/Xatom.h>
  62. #include <X11/Xos.h>
  63. #include "PixmapP.h"
  64.     
  65. #include <stdio.h>
  66. #include <math.h>
  67.  
  68. #define XtStrlen(s)                   ((s) ? strlen(s) : 0)
  69. #define abs(x)                        (((x) > 0) ? (x) : -(x))
  70. #define min(x, y)                     (((x) < (y)) ? (x) : (y))
  71. #define max(x, y)                     (((x) > (y)) ? (x) : (y))
  72.  
  73.  
  74. /*****************************************************************************
  75.  *                               Cut and Paste                               *
  76.  *****************************************************************************/
  77.  
  78.  
  79. Boolean ConvertSelection(w, selection, target, type, value, length, format)
  80.     Widget w;
  81.     Atom *selection, *target, *type;
  82.     XtPointer *value;
  83.     unsigned long *length;
  84.     int *format;
  85. {
  86.     PixmapWidget PW = (PixmapWidget) w;
  87.     Pixmap *pixmap;
  88.     XImage *image;
  89.     Dimension width, height;
  90.  
  91.     switch (*target) {
  92. /*
  93.     case XA_TARGETS:
  94.     *type = XA_ATOM;
  95.     *value = (XtPointer) pixmapClassRec.pixmap_class.targets;
  96.     *length = pixmapClassRec.pixmap_class.num_targets;
  97.     *format = 32;
  98.     return True;
  99. */
  100.     case XA_BITMAP:
  101.     case XA_PIXMAP:
  102.     if (!PWQueryMarked(w)) return False;
  103.     width = PW->pixmap.mark.to_x - PW->pixmap.mark.from_x + 1;
  104.     height = PW->pixmap.mark.to_y - PW->pixmap.mark.from_y + 1;
  105.     image = CreatePixmapImage(PW, width, height);
  106.     CopyImageData(PW->pixmap.image, image, 
  107.               PW->pixmap.mark.from_x, PW->pixmap.mark.from_y,
  108.               PW->pixmap.mark.to_x, PW->pixmap.mark.to_y, 0, 0);
  109.     pixmap = (Pixmap *) XtMalloc(sizeof(Pixmap));
  110.     *pixmap = GetPixmap(PW, image);
  111.     DestroyPixmapImage(&image);
  112.     *type = XA_PIXMAP;
  113.     *value = (XtPointer) pixmap;
  114.     *length = 1;
  115.     *format = 32;
  116.     return True;
  117.  
  118.     case XA_STRING:
  119.     *type = XA_STRING;
  120.     *value = "Hello world!\n";
  121.     *length = XtStrlen(*value);
  122.     *format = 8;
  123.     return True;
  124.  
  125.     default:
  126.     return False;
  127.     }
  128. }
  129.  
  130. void LoseSelection(w, selection)
  131.     Widget w;
  132.     Atom *selection;
  133. {
  134.     PixmapWidget PW = (PixmapWidget) w;
  135.  
  136.     if (_PWDEBUG)
  137.     fprintf(stderr, "Lost Selection\n");
  138.     PW->pixmap.selection.own = False;
  139.     PWUnmark(w);
  140. }
  141.  
  142. void SelectionDone(w, selection, target)
  143.     Widget w;
  144.     Atom *selection, *target;
  145. {
  146.     PixmapWidget PW = (PixmapWidget) w;
  147. /*  
  148.     if (*target != XA_TARGETS)
  149.     XtFree(PW->pixmap.value);
  150. */
  151. }
  152.  
  153. #if NeedFunctionPrototypes
  154. void PWGrabSelection(Widget w, Time time)
  155. #else
  156. void PWGrabSelection(w, time)
  157.     Widget w;
  158.     Time time;
  159. #endif
  160. {
  161.     PixmapWidget PW = (PixmapWidget) w;
  162.  
  163.     PW->pixmap.selection.own = XtOwnSelection(w, XA_PRIMARY, time,
  164.                           ConvertSelection, 
  165.                           LoseSelection, 
  166.                           SelectionDone);
  167.     if (_PWDEBUG && PW->pixmap.selection.own)
  168.         fprintf(stderr, "Own the selection\n");
  169. }
  170.  
  171. void SelectionCallback(w, client_data, selection, type, value, length, format)
  172.     Widget w;
  173.     XtPointer client_data;
  174.     Atom *selection, *type;
  175.     XtPointer value;
  176.     unsigned long *length;
  177.     int *format;
  178. {
  179.     PixmapWidget PW = (PixmapWidget) w;
  180.     Pixmap *pixmap;
  181.  
  182.    switch (*type) {
  183.     
  184.     case XA_BITMAP:
  185.     case XA_PIXMAP:
  186.     DestroyPixmapImage(&PW->pixmap.storage);
  187.     pixmap = (Pixmap *) value;
  188.     PW->pixmap.storage = GetImage(PW, *pixmap);
  189.     XFreePixmap(XtDisplay(w), *pixmap);
  190.     break;
  191.     
  192.     case XA_STRING:
  193.     if (_PWDEBUG)
  194.         fprintf(stderr, "Received:%s\n", value);
  195.     break;
  196.  
  197.     default:
  198.     XtAppWarning(XtWidgetToApplicationContext(w),
  199.              " selection request failed.  PixmapWidget");
  200.     break;
  201.     }
  202.  
  203.     PW->pixmap.selection.limbo = FALSE;
  204. }
  205.  
  206. #if NeedFunctionPrototypes
  207. void PWRequestSelection(Widget w, Time time, Boolean wait)
  208. #else
  209. void PWRequestSelection(w, time, wait)
  210.     Widget w;
  211.     Time time;
  212.     Boolean wait;
  213. #endif
  214. {
  215.     PixmapWidget PW = (PixmapWidget) w;
  216.     
  217.     XtGetSelectionValue(w, XA_PRIMARY, XA_PIXMAP,
  218.             SelectionCallback, NULL, time);
  219.  
  220.     PW->pixmap.selection.limbo = TRUE;
  221.  
  222.     if (wait)
  223.     while (PW->pixmap.selection.limbo) {
  224.         XEvent event;
  225.         XtAppNextEvent(XtWidgetToApplicationContext(w), &event);
  226.         XtDispatchEvent(&event);
  227.     }
  228. }
  229.  
  230. /*****************************************************************************/
  231.